//index.js
const host= require('../../config').host;
var WxParse = require('../../wxParse/wxParse.js');
const getCurrentPagesApi = require('../../utils/common').getCurrentPagesApi;
// var html = '';
//获取应用实例
const app = getApp()

Page({
  data: {
    content:[]

  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  shikuang_detail: function(e) {
    wx.navigateTo({
      url: `../shikuang_detail/shikuang_detail?id=${e.currentTarget.dataset.id}`
    })
  },
  onShareAppMessage: function (res) {
    if (res.from === 'menu') {
      return {
        title: '国数源',
        path: getCurrentPagesApi()
      }
    }
    
  },
  huiyuan: function(e){
    var that = this
    var ee = e
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        app.globalData.code = res.code
      }
    })
    
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            withCredentials: true,
            // lang: "zh_TW",
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              console.log(res);
              app.globalData.userInfo = res.userInfo
              wx.request({
                url: host + 'api/tourist/weixin_login',
                data: {
                    encryptedData: res.encryptedData,
                    iv: res.iv,
                    code: app.globalData.code
                },
                header: {
                    'content-type': 'application/json'
                },
                success: res => {
                    // //从数据库获取用户信息
                    // // that.queryUsreInfo();
                    // console.log(res.data.data);
                      if(res.data.sta == 1){
                        // this.globalData.userType = res.data.data.huiyuan[0].type
                        app.globalData.uuid = res.data.data.uuid
                      }else{
                        wx.showModal({
                            title:'提示',
                            content:'登录出错',
                            showCancel:false,
                            confirmText:'返回授权',
                            success: res => {
                                if (res.confirm) {
                                    console.log('用户点击了“返回授权”')
                                } 
                            }
                        })
                      }
                    }
                });
              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              // if (this.userInfoReadyCallback) {
              //   this.userInfoReadyCallback(res)
              // }
            }
          })
        }
      },
      fail: function(){
        // wx.navigate
      }
    }),
// console.log(e.currentTarget.dataset.type);
    wx.request({
      // console.log(e.currentTarget.dataset.type);
      url: host + "api/memberdingdan/pay",
      data: {type: e.currentTarget.dataset.type,uuid:app.globalData.uuid},
      success: function(res){
        var data = res.data
        // console.log(data.timeStamp);
        // console.log(data);
        wx.requestPayment({
          "nonceStr": data.nonceStr,
          "timeStamp": data.timeStamp,
          "package": data.package,
          "signType": "MD5",
          "paySign": data.paySign,
          success:function(res){
            wx.showModal({
                title:'提示',
                content:'支付成功',
                showCancel:false,
                confirmText:'继续浏览',
                success: res => {
                  app.globalData.userType = e.currentTarget.dataset.type
                    wx.navigateTo({
                      url: app.globalData.huiyuanpath
                    }) 
                }
            })
          },
          fail:function(res){
            wx.showModal({
                title:'提示',
                content:'取消支付',
                showCancel:true,
                confirmText:'继续支付',
                cancelText:'取消',
                success: res => {
                  if (res.confirm) {
                    that.huiyuan(ee);
                  } else if (res.cancel) {
                    
                  }
                }
            })
          }
        })
      }
    })
  },
  onLoad: function (params) {
    var id = params.id
  	this.ready(id);
  },
  ready: function(id) {
    var that = this
      wx.request({
        url: host+"api/index/kaitonghuiyuan",
        data:{id: id},
        success: function(res){
          // console.log(res.data.xuzhi[0].value)
          // html = res.data[0].content
          that.setData({
            content: res.data
          }),
          WxParse.wxParse('article', 'html', res.data.xuzhi[0].value, that, 5)
        },
        fail: function(data){
          console.log(data)
        }
      })

  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})
